<template>
  <el-dialog
    class="addedit-project-team-member-dialog"
    width="450px"
    :visible.sync="dialogVisible"
    :title="operType == 'add' ? '添加项目组成员' : '修改项目组成员'"
    :close-on-click-modal="false"
    :append-to-body="true"
    v-dialogDrag="true"
    @open="handleDialogOpen"
    @closed="handleDialogClosed"
  >
    <el-form ref="formRef" :model="formData" :rules="formRules" size="small" label-width="100px">
      <el-form-item label="排序" prop="sort">
        <el-input-number
          style="width: 200px;"
          v-model="formData.sort"
          placeholder="请输入排序"
          :min="0"
          :precision="0"
        ></el-input-number>
      </el-form-item>
      <el-form-item label="姓名" prop="name">
        <el-input
          style="width: 100%;"
          v-model="formData.name"
          placeholder="请输入姓名"
          :clearable="true"
        ></el-input>
      </el-form-item>
      <el-form-item label="职称" prop="job">
        <el-input
          style="width: 100%;"
          v-model="formData.job"
          placeholder="请输入职称"
          :clearable="true"
        ></el-input>
      </el-form-item>
      <el-form-item label="资格证书号" prop="certificate">
        <el-input
          style="width: 100%;"
          v-model="formData.certificate"
          placeholder="请输入资格证书号"
          :clearable="true"
        ></el-input>
      </el-form-item>
    </el-form>
    <div class="dialog-footer" slot="footer">
      <el-button type="primary" @click="handleSubmit">确定</el-button>
      <el-button @click="dialogVisible = false">取消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import {
  reqAddProjectTeamMember,
  reqEditProjectTeamMember
} from '@/api/person/projectTeamMembers.js'

export default {
  name: 'AddEditProjectTeamMember',
  dicts: [],
  props: {
    show: {
      type: Boolean,
      default: false,
    },
    operType: {
      type: String,
      default: 'add',
    },
    row: {
      type: Object,
      default: () => ({})
    },
    sort: {
      type: Number,
      default: 0
    }
  },
  computed: {
    dialogVisible: {
      get() {
        return this.show
      },
      set(value) {
        this.$emit('update:show', value)
      }
    }
  },
  data() {
    return {
      formData: {},
      formRules: {
        sort: [
          { required: true, message: '请输入排序', trigger: 'blur' }
        ],
        name: [
          { required: true, message: '请输入姓名', trigger: 'blur' }
        ],
        job: [
          { required: true, message: '请输入职称', trigger: 'blur' }
        ],
        certificate: [],
      }
    }
  },
  created() {
    this.initFormData()
  },
  methods: {
    // 弹框打开时
    handleDialogOpen() {
      if (this.operType == 'add') {
        this.formData.sort = this.sort + 1
      } else if (this.operType == 'edit') {
        for (let key in this.formData) {
          this.formData[key] = this.row[key]
        }
      }
    },
    // 弹框关闭时
    handleDialogClosed() {
      this.initFormData()
      this.$refs.formRef.resetFields()
    },
    // 初始化表单数据
    initFormData() {
      this.formData = {
        sort: null,
        name: '',
        job: '',
        certificate: '',
      }
    },
    // 获取表单数据
    getFormData() {
      const params = JSON.parse(JSON.stringify(this.formData))
      if (this.operType == 'edit') {
        params.taskGroupMemberId = this.row.taskGroupMemberId
      }
      return params
    },
    // 点击确定
    handleSubmit() {
      this.$refs.formRef.validate((valid) => {
        if (!valid) return
        let fn = null
        if (this.operType == 'add') fn = reqAddProjectTeamMember
        else if (this.operType == 'edit') fn = reqEditProjectTeamMember

        const data = this.getFormData()
        fn(data).then(res => {
          this.$message.success('操作成功')
          this.dialogVisible = false
          this.$emit('refresh')
        }).catch(err => {})
      })
    }
  },
}
</script>

<style lang="scss" scoped>
.addedit-project-team-member-dialog {

  ::v-deep .el-dialog__body {
    max-height: 70vh;
    overflow-y: auto;
  }
}
</style>